﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace ParsecParser
{
    [Serializable]
    public class CombatStatistics
    {
        public string CharacterName { get; set; }
        public int Activates { get; set; }
        public int AbsorbShieldsCast { get; set; }
        public double AbsorbShieldValue { get; set; }
        public long DamageOut { get; set; }
        public long DamageIn { get; set; }
        public long AbsorbedIn { get; set; }
        public long HealOut { get; set; }
        public long EffectiveHealOut { get; set; }
        public long HealIn { get; set; }
        public long Threat { get; set; }
        public long CombatTicks { get; set; }
        public DateTime StartCombat { get; set; }
        public DateTime? EndCombat { get; set; }
        public RaidEncounterDetails RaidEncounterDetail { get; set; }
        public TimeSpan CombatTimeSpan { get; set; }

        protected object ParticipantsLock = new object();
        protected Dictionary<string, CombatLogParticipant> Participants { get; set; }

        public CombatStatistics()
        {
            RaidEncounterDetail = new RaidEncounterDetails();
            ParticipantsLock = new object();
            Participants = new Dictionary<string, CombatLogParticipant>();
        }
        public CombatStatistics(Dictionary<string, CombatLogParticipant> partipants)
        {
            RaidEncounterDetail = new RaidEncounterDetails();
            Participants = partipants ?? new Dictionary<string, CombatLogParticipant>();
        }

        public void AddOrUpdateParticipant(CombatLogParticipant p)
        {
            if (p != null)
            {
                string k = p.GetUniqueKey();
                if (k != null)
                {
                    lock (ParticipantsLock)
                    {
                        Participants[k] = p;
                    }
                }
            }
        }
        public void RemoveParticipant(CombatLogParticipant p)
        {
            if (p != null)
            {
                string k = p.GetUniqueKey();
                if (k != null)
                {
                    lock (ParticipantsLock)
                    {
                        Participants.Remove(k);
                    }
                }
            }
        }
        public void RemoveParticipant(string key)
        {
            if (key != null)
            {
                lock (ParticipantsLock)
                {
                    Participants.Remove(key);
                }
            }
        }
        public List<string> GetParticipantKeys()
        {
            lock (ParticipantsLock)
            {
                return new List<string>(Participants.Keys);
            }
        }
        public List<CombatLogParticipant> GetParticipantList()
        {
            lock (ParticipantsLock)
            {
                return Participants.Values.ToList();
            }
        }
        public Dictionary<string, CombatLogParticipant> GetParticipants()
        {
            lock (ParticipantsLock)
            {
                return Participants.ToDictionary(x => x.Key, x => x.Value);
            }
        }
        public CombatLogParticipant GetParticipant(string key)
        {
            CombatLogParticipant p = null;
            if (key != null)
            {
                lock (ParticipantsLock)
                {
                    Participants.TryGetValue(key, out p);
                }
            }
            return p;
        }
    }
}
